<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <!-- 搜索输入框 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <!-- 添加按钮 -->
          <el-input placeholder="请输入内容" class="input-with-select" v-model="queryData.query" clearable @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="$router.push('/goods/add')">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品列表 -->
      <el-table border stripe :data="goods">
        <el-table-column header-align="center" type="index" label="#"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column width="100" label="商品价格" prop="goods_price"></el-table-column>
        <el-table-column width="100" label="商品重量" prop="goods_weight"></el-table-column>
        <el-table-column width="120" label="添加时间">
          <template slot-scope="scope">
            {{scope.row.upd_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column width="150" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeGoods(scope.row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页功能 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryData.pagenum"
        :page-sizes="[5, 10, 15, 20, 30]"
        :page-size="queryData.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      queryData: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      goods: [],
      total: 0
    }
  },
  methods: {
    async getGoodsList () {
      const { data: res } = await this.$axios.get('goods', {
        params: this.queryData
      })
      if (res.meta.status !== 200) return this.$message.error('获取失败！')
      this.goods = res.data.goods
      this.total = res.data.total
      // console.log(res)
    },
    // 分页每页显示多少点击事件
    handleSizeChange (val) {
      this.queryData.pagesize = val
      this.getGoodsList()
    },
    // 页码点击事件
    handleCurrentChange (val) {
      this.queryData.pagenum = val
      this.getGoodsList()
    },
    removeGoods (id) {
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$axios.delete('goods/' + id)
        if (res.meta.status !== 200) return this.$message.error('删除商品失败！')
        this.getGoodsList()
        this.$message.success('删除成功!')
      }).catch(() => {
        this.$message('已取消删除')
      })
    }
  },
  computed: {
  },
  created () {
    this.getGoodsList()
  }
}
</script>

<style lang='less' scoped>

</style>
